package org.nokatag.system;

import java.sql.Connection;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSession;
import org.hibernate.Session;
import org.hibernate.Transaction;



public class ServletNokaContext extends NokaContext{
	private static final long serialVersionUID = 5238426444698034070L;

    public static final String HTTP_REQUEST = "org.noka.context.HttpServletRequest";
    public static final String HTTP_RESPONSE = "org.noka.context.HttpServletResponse";
    public static final String HTTP_SESSION = "org.noka.context.HttpSession";
    public static final String SERVLET_DISPATCHER = "org.noka.context.ServletDispatcher";
    public static final String SERVLET_CONTEXT = "org.noka.context.ServletContext";
    public static final String PAGE_CONTEXT = "org.noka.context.PageContext";
    public static final String REQUEST_CONNECTION = "org.noka.context.connection";
    public static final String HIBERNATE_SESSION = "org.noka.hibernate.session";
    public static final String IBATIS_SESSION = "org.noka.ibatis.session";
    public static final String HIBERNATE_TRANSACTION="org.noka.hibernate.Transaction";
    public static final String HIBERNATE_TRANSACTION_BREAK="org.noka.hibernate.Transaction_break";
    public static final String DATA_SOURCE = "org.noka.data.source";
    
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private ServletNokaContext(Map context) {
		super(context);
	}

	public static PageContext getPageContext() {
		return (PageContext) NokaContext.getContext().get(PAGE_CONTEXT);
	}

	public static HttpServletRequest getRequest() {
		return (HttpServletRequest) NokaContext.getContext().get(HTTP_REQUEST);
	}

	public static HttpServletResponse getResponse() {
		return (HttpServletResponse) NokaContext.getContext().get(HTTP_RESPONSE);
	}

	public static ServletContext getServletContext() {
		return (ServletContext) NokaContext.getContext().get(SERVLET_CONTEXT);
	}

	public static Connection getConnection() {
		return (Connection) NokaContext.getContext().get(REQUEST_CONNECTION);
	}

	public static void setConnection(Connection con) {
		NokaContext.getContext().put(REQUEST_CONNECTION, con);
	}

	public static HttpSession getHttpSession() {
		return (HttpSession) NokaContext.getContext().get(HTTP_SESSION);
	}

	public static Session getHibernateSession() {
		return (Session) NokaContext.getContext().get(HIBERNATE_SESSION);
	}

	public static void setHibernateSession(Session session) {
		NokaContext.getContext().put(HIBERNATE_SESSION, session);
	}

	public static SqlSession getIbatisSession() {
		return (SqlSession) NokaContext.getContext().get(IBATIS_SESSION);
	}

	public static void setIbatisSession(SqlSession session) {
		NokaContext.getContext().put(IBATIS_SESSION, session);
	}

	public static void setTransaction(Transaction tt) {
		NokaContext.getContext().put(HIBERNATE_TRANSACTION, tt);
	}

	public static Transaction getTransaction() {
		Object tt = NokaContext.getContext().get(HIBERNATE_TRANSACTION);
		if (null != tt && tt instanceof Transaction) {
			return (Transaction) tt;
		}
		return null;
	}

	public static void setTransactionBreak(Boolean tt) {
		NokaContext.getContext().put(HIBERNATE_TRANSACTION_BREAK, tt);
	}

	public static Boolean getTransactionBreak() {
		Object obj = NokaContext.getContext().get(HIBERNATE_TRANSACTION_BREAK);
		if (null != obj && obj instanceof Boolean) {
			return (Boolean) obj;
		}
		return false;
	}
	
	public static void setDataSource(DataSource dataSource){
		NokaContext.getContext().put(DATA_SOURCE, dataSource);
	}
	
	public static DataSource getDataSource(){
		return (DataSource)NokaContext.getContext().get(DATA_SOURCE);
	}
	
	    
}
